package com.nf.health.app.homepage.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.nf.health.app.R;
import com.nf.health.app.login.activity.LoginActivity;
import com.nf.health.app.activity.global.NoBaseActivity;
import com.nf.health.app.adapter.HealthCheckAdapter2;
import com.nf.health.app.models.CheckItem;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.PreferenceHelper;
import com.nf.health.app.utils.ViewUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author Ian
 * @ClassName: HealthyCheckedActivity
 * @Description: 健康自测
 * @date 2015-11-19 下午1:19:07
 */
public class HealthSelfTestingActivity extends NoBaseActivity {
    @Bind(R.id.listview)
    ListView listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.fragment_base);
        ButterKnife.bind(this);
        ViewUtil.setText(this, R.id.titlebar_title_tv, "健康自测");

        listview.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CheckItem item = (CheckItem) parent.getItemAtPosition(position);
                if (item == null) {
                    return;
                }
                if ("-1".equals(item.getType())) {//中医体检
                    String username = PreferenceHelper.getString(PreferenceHelper.USERNAME, "");
                    if(TextUtils.isEmpty(username)){
                        username = PreferenceHelper.getString(PreferenceHelper.USERNICK, "");
                    }
                    if (!TextUtils.isEmpty(username)) {
                        //TODO 测试账号只有3位
                        boolean isLogin = PreferenceHelper.getBoolean(PreferenceHelper.ISLOGIN, true);
                        if (isLogin) {
                            ActivityUtils.showActivity(HealthSelfTestingActivity.this, DoctorDetectionActivity.class);
                        } else {
                            PreferenceHelper.putBoolean(PreferenceHelper.ISLOGIN, false);
                            //showToast("未登录");
                            ActivityUtils.showActivity(HealthSelfTestingActivity.this, LoginActivity.class);
                        }
                    } else {
                        PreferenceHelper.putBoolean(PreferenceHelper.ISLOGIN, false);
                        //showToast("未登录");
                        ActivityUtils.showActivity(HealthSelfTestingActivity.this, LoginActivity.class);
                    }

                } else if ("-2".equals(item.getType())) {//健康评估
                    String username = PreferenceHelper.getString(PreferenceHelper.USERNAME, "");
                    if(TextUtils.isEmpty(username)){
                        username = PreferenceHelper.getString(PreferenceHelper.USERNICK, "");
                    }
                    if (!TextUtils.isEmpty(username)) {
                        //TODO 测试账号只有3位
                        boolean isLogin = PreferenceHelper.getBoolean(PreferenceHelper.ISLOGIN, true);
                        if (isLogin) {
                            ActivityUtils.showActivity(HealthSelfTestingActivity.this, RiskEvaluationActivity.class);
                        } else {
                            PreferenceHelper.putBoolean(PreferenceHelper.ISLOGIN, false);
                            //showToast("未登录");
                            ActivityUtils.showActivity(HealthSelfTestingActivity.this, LoginActivity.class);
                        }
                    } else {
                        PreferenceHelper.putBoolean(PreferenceHelper.ISLOGIN, false);
                        //showToast("未登录");
                        ActivityUtils.showActivity(HealthSelfTestingActivity.this, LoginActivity.class);
                    }

                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString("evalFlag", item.getType());
                    bundle.putString("desc", item.getDescs());
                    bundle.putString("title", item.getTitle());
                    ActivityUtils.showActivityForResult(HealthSelfTestingActivity.this, StartCheckActivity.class, bundle, 1);
                }
            }
        });

        provider.requestCheckList("checklist");
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        if (actionName.equals("checklist")) {
            List<CheckItem> result = (List<CheckItem>) object;
            HealthCheckAdapter2 adapter =  new HealthCheckAdapter2(HealthSelfTestingActivity.this, result);
            listview.setAdapter(adapter);
            //listview.setAdapter(new HealthCheckAdapter(listview, result, R.layout.item_check));
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

}
